package com.henry.chatkit.messages.holder

import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.henry.chatkit.R
import com.henry.chatkit.glide.loadChatImage
import com.henry.chatkit.messages.MessageInfo
import com.henry.chatkit.MessageType
import com.henry.chatkit.utils.ImageUtils
import com.henry.chatkit.utils.SizeUtils
import java.util.*

class MessageImageHolder(itemView: View) : MessageContentHolder(itemView) {

    private val downloadEles: List<String> = ArrayList()
    private val mClicking = false
    private lateinit var contentImage: ImageView
    private lateinit var videoPlayBtn: ImageView
    private lateinit var videoDurationText: TextView

    override fun getVariableLayout(): Int {
        return R.layout.message_content_image
    }

    override fun initVariableViews() {
        contentImage = rootView.findViewById(R.id.content_image_iv)
        videoPlayBtn = rootView.findViewById(R.id.video_play_btn)
        videoDurationText = rootView.findViewById(R.id.video_duration_tv)
    }

    override fun layoutVariableViews(msg: MessageInfo?, position: Int) {

        if (msg == null) {
            return
        }

        msgContentFrame.background = null
        when (msg.msgType) {
            MessageType.CUSTOM_FACE, MessageType.CUSTOM_FACE + 1 ->
                performCustomFace(msg, position)
            MessageType.IMAGE, MessageType.IMAGE + 1 ->
                performImage(msg, position)
            MessageType.VIDEO, MessageType.VIDEO + 1 ->
                performVideo(msg, position)
        }
    }

    private fun performCustomFace(msg: MessageInfo, position: Int) {
        videoPlayBtn.visibility = View.GONE
        videoDurationText.visibility = View.GONE
        val params = RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        params.addRule(RelativeLayout.CENTER_IN_PARENT)
        contentImage.layoutParams = params
    }

    private fun performImage(msg: MessageInfo, position: Int) {
        contentImage.layoutParams = getImageParams(contentImage.layoutParams, msg)
        resetParentLayout()
        videoPlayBtn.visibility = View.GONE
        videoDurationText.visibility = View.GONE
        contentImage.loadChatImage(msg.path, contentImage.layoutParams.width, contentImage.layoutParams.height, properties.imageRadius)
    }

    private fun performVideo(msg: MessageInfo, position: Int) {

    }

    private fun getImageParams(params: ViewGroup.LayoutParams, msg: MessageInfo): ViewGroup.LayoutParams {

        val sizes = ImageUtils.getThumbImageSize(msg.imgWidth, msg.imgHeight, properties.imageMinSize, properties.imageMaxSize)
        params.width = sizes[0]
        params.height = sizes[1]

        return params
    }

    private fun resetParentLayout() {
        (contentImage.parent.parent as FrameLayout).setPadding(8, 0, 8, 0)
    }

}